import { Snackbar } from 'uino-tarsier';
// import {get} from '../i18n';

const get = (msg: string) => msg;

const queueSize = 3; //一次显示几个

const snackbar = new Snackbar({
  queueSize,
  autoHideDuration: 3000, // 设置自动消失时间，如果不设置默认为 5000
});

export type EventName = 'warn' | 'success' | 'error';

/**
 * 公用提示框
 * @param eventName success，warn，error
 * @param message 提示
 */
export function alert(eventName: EventName, message: string) {
  if (snackbar[eventName]) {
    snackbar[eventName](get(message));
  } else {
    console.error(`not find eventName: ${eventName}`);
  }
}

export function success(message: string) {
  alert('success', message);
}

export function warn(message: string) {
  alert('warn', message);
}

export function error(message: string) {
  alert('error', message);
}

export type LimitMessageFunc = () => Promise<any>;

let curTaskCount = 0;
export async function limitMessage(arr: LimitMessageFunc[]) {
  let func: LimitMessageFunc | undefined;
  while (curTaskCount < queueSize && (func = arr.shift())) {
    curTaskCount++;
    func().then(() => {
      curTaskCount--;
      limitMessage(arr);
    }, () => {
      curTaskCount--;
      limitMessage(arr);
    });
  }
}
